<?php
/* 
    Project     : 48c6c450f1a4a0cc53d9585dc0fee742
    Created on  : Mar 16, 2013, 11:29:15 PM
    Author      : Truong Khuong - khuongxuantruong@gmail.com
    Description :
        Purpose of the stylesheet follows.
*/
class category_model extends backend_Model {

    function __construct()
    {
        parent::__construct('categories','');
    }
    function get_by_type($type=null){
        if($type!=null)$this->db->where('Type',$type);
        $query=$this->db
            ->where('Cat',1)
            ->order_by('Type','ASC')
            ->order_by('Parent','ASC')
            ->order_by('Position','ASC')
            ->order_by('Insert','ASC')
            ->get("categories"); 
        return $query->result();
    }
    function buildTree(array $elements, $parentId = 0,$parents=array(0)) {
        $branch = array();
        foreach ($elements as $element) {
            if ($element->Parent == $parentId) {
                $tmp=$parents;$tmp[]=$element->Id;
                $children = $this->buildTree($elements, $element->Id,$tmp);
                if ($children) {
                    $element->Children = $children;
                }
                $element->Parents=$parents;
                $branch[] = $element;
            }
        }
        return $branch;
    }
    function getTreeNode($id,$tree){
        
    }
    function buildTreeArray(array $elements, $parentId = 0,$level=0,$parent_title='',$path='') {
        if($parentId==0){
            for ($i=0;$i<count($elements);$i++) {
                $f=false;
                for ($j=0;$j<count($elements);$j++) {
                    if($elements[$i]->Parent==$elements[$j]->Id && $elements[$i]->Type==$elements[$j]->Type){
                        $f=true;
                        break;
                    }
                }
                if($f==false){
                    $elements[$i]->Parent=0;
                    $elements[$i]->ParentTitle='';
					$elements[$i]->error=true;
					$elements[$i]->Path='';
                }
                if($elements[$i]->Parent==$elements[$i]->Id){
                    $elements[$i]->Parent=0;
                    $elements[$i]->ParentTitle='';
					$elements[$i]->Path='';
					$elements[$i]->error=true;
                }
            }
        }
        $branch = array();
        foreach ($elements as $element) {
            if ($element->Parent == $parentId) {
                $element->Level=$level;$element->ParentTitle=$parent_title;
                $element->Display=repeater('----',$level).$element->Title;
				$element->Display=$path."/".$element->Title;
                $branch[] = $element;
                $children = $this->buildTreeArray($elements, $element->Id,$level+1,$element->Title,$path.'/'.$element->Title);
                if (!empty($children))foreach ($children as $ch){
                    $branch[] = $ch;
                }
            }
        }
        return $branch;
    }
    function binding($type=null){
        $where = "WHERE  Cat=1";
        if($type!=null){
            $where = "WHERE  Type='$type' AND Cat=1";
        }
            $this->datatables_config=array(
                    "table"=>"categories",
                    "where"     =>$where,
                    "order_by"=>"ORDER BY `Type` ASC, `Parent` ASC, `Position` ASC,`Insert` ASC  ",
                    "columnmaps"=>array(
                            
                    )
            );
            return $this->_bindingdata();
    }
    function update_node_by_parent($Parent=0,$NewParent=0){
        $this->db->set('Update', 'NOW()', FALSE);
        $this->db->where('Parent', $Parent);
        @$this->db->update('categories', array('Parent'=>$NewParent));
        @$count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if ($count > 0)
            return true;
        return false;
    }
    
}
?>
